package cs2340.todo.view;

import java.util.Calendar;

import cs2340.todo.model.Category;
import cs2340.todo.model.TODOItem;
import cs2340.todo.model.TODOManager;
import cs2340.todo.model.User;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

/**
 * 
 * @author Vertigo
 *
 */
public class NewItemActivity extends Activity {

	private Button btnDone, btnCancel;
	private EditText txtTitle, txtDescription, txtDate, txtTime, txtLocation;
	private Spinner spinCategory;
	private String title, description;
	private Location location;
	private Category category;
	private int mYear, mMonth, mDay;
	private int mHour, mMinute, mAmPm;
	private String ampm;
	private Calendar time;
	private User user;
	private TODOManager manager;
	
	
	private static final int DATE_DIALOG_ID = 0;
	private static final int TIME_DIALOG_ID = 1;

	/**
	 * Called when the activity is created
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_item);
		
		//get user info from TODOManagerActivity
		Bundle getU = getIntent().getExtras();
        user = new User(getU.getString("name"), getU.getString("username"), getU.getString("email"), getU.getString("password"));

		btnDone = (Button) findViewById(R.id.btnDone);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		txtTitle = (EditText) findViewById(R.id.txtTitle);
		txtDescription = (EditText) findViewById(R.id.txtDescription);
		txtDate = (EditText) findViewById(R.id.txtDate);
		txtTime = (EditText) findViewById(R.id.txtTime);
		txtLocation = (EditText) findViewById(R.id.txtLocation);
		spinCategory = (Spinner) findViewById(R.id.spinCategory);
	
		//Category spin adapter
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		this, R.array.categories_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinCategory.setAdapter(adapter);
		
		manager = new TODOManager(NewItemActivity.this);

		btnDone.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				title = txtTitle.getText().toString().trim();
				description = txtDescription.getText().toString().trim();
				time = Calendar.getInstance();
				time.set(mYear, mMonth, mDay, mHour, mMinute, 0);
				location = new Location(txtLocation.getText().toString().trim());
				category = new Category(spinCategory.getSelectedItem().toString());
				
				TODOItem newItem = new TODOItem(user.getUsername(), title, description, (int)time.getTimeInMillis(), location, category);
				
				
				if (checkItem()) {
					manager.open();
					manager.addItem(user.getUsername(), newItem);
				    for (TODOItem i : manager.getUserItems(user)) {
						Log.d("items", i.toString());
					}
					manager.close();
					
					AlertDialog newItemCreated = new AlertDialog.Builder(NewItemActivity.this).create();
					newItemCreated.setTitle("New Item Created!");
					newItemCreated.setMessage("Your task has been created.");
					newItemCreated.setButton("Continue", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							setResult(RESULT_OK);
							finish();
						}
					});
					newItemCreated.show();

				}
			}
		});
		
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		});

		txtDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});
		
		txtTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		});

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR);
		mMinute = c.get(Calendar.MINUTE);
		mAmPm = c.get(Calendar.AM_PM);
		if (mAmPm > 0)
			ampm = "pm";
		else
			ampm = "am";
		
		spinCategory.setOnItemSelectedListener(new MyOnItemSelectedListener());
		updateDisplay();
	}

	/**
	 * checks if new item info is correct
	 * @return boolean
	 */
	public boolean checkItem() {
		return true;
	}

	/**
	 * update the display after opening a dialog
	 */
	private void updateDisplay() {
		txtDate.setText(
				new StringBuilder()
				.append(mMonth + 1).append("/")
				.append(mDay).append("/")
				.append(mYear).append(" "));
		txtTime.setText(
				new StringBuilder()
				.append(padHour(mHour)).append(":")
				.append(padMinute(mMinute)).append(ampm));
	}
	
	/**
	 * displays the hour in 12hour format
	 * @param c
	 * @return c as a string
	 */
	private static String padHour(int c) {
		if (c > 12)
			c -= 12;
		return String.valueOf(c);
	}
	
	/**
	 * adds a placeholder "0" if minutes are less than 10
	 * @param c
	 * @return c as a string
	 */
	private static String padMinute(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	/**
	 * DatePicker dialog
	 */
	private DatePickerDialog.OnDateSetListener mDateSetListener =
			new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, 
				int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};

	/**
	 * TimePicker dialog
	 */
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
			new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;
			if (mHour > 12) {
				mAmPm = 1;
				ampm = "pm";
			} else {
				mAmPm = 0;
				ampm = "am";
			}
			updateDisplay();
		}
	};
	
	/**
	 * custom dialog for creating new category
	 */
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this,
					mDateSetListener,
					mYear, mMonth, mDay);
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this,
					mTimeSetListener, mHour, mMinute, false);
		}
		return null;
	}
	
	/**
	 * 
	 * @author Vertigo
	 *
	 */
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	    	
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}

}